//
//  PhotoView.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/17.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "PhotoView.h"

#import <Photos/Photos.h>
#import "PhotoTool.h"
#import "PhotoBrowserTableViewController.h"
#import "MBProgressHUD.h"
#import "UIView+FrameView.h"
#import "PhotoCollectionViewCell.h"
#import "ShowBigImageViewController.h"


typedef void(^handler)(NSArray<UIImage *> *selectPhotos, NSArray<PhotoModel *> *selectPhotoModles);

@interface PhotoView ()<UIImagePickerControllerDelegate,
                        UINavigationControllerDelegate,
                        UICollectionViewDelegateFlowLayout,
                        PHPhotoLibraryChangeObserver,
                        UICollectionViewDelegate,
                        UICollectionViewDataSource>

@property (strong, nonatomic) NSMutableArray<PhotoModel *> *arraySelectPhotos;
@property (strong, nonatomic) NSMutableArray<PHAsset *> *arrayDataSources;

@property (assign, nonatomic) BOOL isSelectOriginalPhoto;


@property (assign, nonatomic) BOOL animate;

@property (copy, nonatomic) handler handler;




@end

@implementation PhotoView

- (NSMutableArray<PHAsset *> *)arrayDataSources{
    
    if (_arrayDataSources == nil) {
        _arrayDataSources = [NSMutableArray array];
    }
    return _arrayDataSources;
}

- (NSMutableArray<PhotoModel *> *)arraySelectPhotos{
    
    if (_arraySelectPhotos == nil) {
        _arraySelectPhotos = [NSMutableArray array];
    }
    
    return _arraySelectPhotos;
}

- (UICollectionView *)collectionView{
    
    if (_collectionView == nil) {
        
        CGFloat height = 35;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 3;
        layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.baseView.frame.size.width, self.baseView.frame.size.height -height * 3 - 3 * 1) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [self.baseView addSubview:self.collectionView];
    }
    
    return _collectionView;
}



- (void)dealloc{
    
    [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}

- (instancetype)init{
    
    self = [super init];
    if (self) {
        [self setupSubviews];
        
        self.maxSelectCount = 10;
        self.maxPreviewCount = 20;
        
        [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
        
    }
    return self;
    
}

- (void)setupSubviews{
    
    self.backgroundColor = [UIColor grayColor];
    self.baseView = [[UIView alloc] init];
    self.baseView.backgroundColor = [UIColor clearColor];
    
    [self addSubview:self.baseView];
    
    //取消按钮
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setBackgroundColor:[UIColor whiteColor]];
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.cancelButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:self.cancelButton];
    
    self.cancelLine = [[UIView alloc] init];
    self.cancelLine.backgroundColor = [UIColor blackColor];
    [self.baseView addSubview:self.cancelLine];
    
    //相册按钮
    self.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.photoButton setTitle:@"相册" forState:UIControlStateNormal];
    [self.photoButton setBackgroundColor:[UIColor whiteColor]];
    [self.photoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.photoButton addTarget:self action:@selector(getPhotoFromSystem) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:self.photoButton];
    
    self.photoLine = [[UIView alloc] init];
    self.photoLine.backgroundColor = [UIColor blackColor];
    [self.baseView addSubview:self.photoLine];
    
    //拍照按钮
    self.takephotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.takephotoButton setTitle:@"拍照" forState:UIControlStateNormal];
    [self.takephotoButton setBackgroundColor:[UIColor whiteColor]];
    [self.takephotoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.takephotoButton addTarget:self action:@selector(takePhotoFromCamera) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:self.takephotoButton];
    
    self.takephotoLine = [[UIView alloc] init];
    self.takephotoLine.backgroundColor = [UIColor blackColor];
    [self.baseView addSubview:self.takephotoLine];
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat height = 35;
    CGFloat y = [UIScreen mainScreen].bounds.size.height - 400;
    
    self.x = 0;
    self.y = y;
    self.width = [UIScreen mainScreen].bounds.size.width;
    self.height = 400;
    
    
    
    self.baseView.frame = CGRectMake(0, self.frame.size.height - 300, self.frame.size.width, 300);
    
    self.cancelButton.frame = CGRectMake(0, self.baseView.frame.size.height - height, self.frame.size.width, height);
    self.cancelLine.frame = CGRectMake(0, CGRectGetMinY(self.cancelButton.frame) - 1, self.baseView.frame.size.width, 1);
    
    
    self.photoButton.frame = CGRectMake(0, CGRectGetMinY(self.cancelLine.frame) - height, self.baseView.frame.size.width, height);
    self.photoLine.frame = CGRectMake(0, CGRectGetMinY(self.photoButton.frame) - 1, self.baseView.frame.size.width, 1);
    
    
    self.takephotoButton.frame = CGRectMake(0, CGRectGetMinY(self.photoLine.frame) - height, self.baseView.frame.size.width, height);
    self.takephotoLine.frame = CGRectMake(0, CGRectGetMinY(self.takephotoButton.frame) - 1, self.baseView.frame.size.width, 1);
    
    self.collectionView.frame = CGRectMake(0, 0, self.baseView.frame.size.width, self.baseView.frame.size.height -height * 3 - 3 * 1);
    
}

#pragma mark 相册变化回调

- (void)photoLibraryDidChange:(PHChange *)changeInstance{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self loadPhotoFromAlbum];
        
    });
    
}

#pragma mark button method

- (void)back{
    
    [self.arraySelectPhotos removeAllObjects];
    [self hide];
    
    
}


- (void)getPhotoFromSystem{
    
    if (![self judgeIsHavePhotoAblumAuthority] ) {
        
        
        NSLog(@"123345");
        
        
        
    } else {
        
        self.animate = NO;
        
        PhotoBrowserTableViewController *photoBrowser = [[PhotoBrowserTableViewController alloc] initWithStyle:UITableViewStylePlain];
        
        photoBrowser.maxSelectCount = self.maxSelectCount;
        photoBrowser.arraySelectPhotos = self.arraySelectPhotos;
        
        __weak typeof(photoBrowser) weakBrowser = photoBrowser;
        __weak typeof(self) weakself = self;
        
        [photoBrowser setDoneBlock:^(NSArray<PhotoModel *> *photoModels, BOOL isSelectOriginalPhoto) {
            
            __strong typeof(weakself) strongself = weakself;
            
            NSLog(@"=====1%@-selec = %@",photoModels,weakself.arraySelectPhotos);
            strongself.isSelectOriginalPhoto = isSelectOriginalPhoto;
             NSLog(@"=====2%@-selec = %@",photoModels,weakself.arraySelectPhotos);
//            [strongself.arraySelectPhotos removeAllObjects];
//             NSLog(@"=====3%@-selec = %@",photoModels,weakself.arraySelectPhotos);
//            [strongself.arraySelectPhotos addObjectsFromArray:photoModels];
            
            NSLog(@"=====4%@-selec = %@",photoModels,weakself.arraySelectPhotos);
            
            [weakself requestSelPhotos:weakBrowser];
            
            
        }];
        
        [photoBrowser setCancelBlock:^{
            
            [weakself hide];
            
        }];
        
        [self presentVC:photoBrowser];
        
        
    }
    
    
}

- (void)takePhotoFromCamera{
    
    NSLog(@"%s",__func__);
    
    if (self.arraySelectPhotos.count > 0) {
        [self requestSelPhotos:nil];
    } else {
        
    }
}


- (void)presentVC:(UIViewController *)vController{
    
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:vController];
    navigation.navigationBar.translucent = YES;
    
    [navigation.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [navigation.navigationBar setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:19/255.0 green:153/255.0 blue:231/255.0 alpha:1.0]] forBarMetrics:UIBarMetricsDefault];
    [navigation.navigationBar setTintColor:[UIColor whiteColor]];
    navigation.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.sender presentViewController:navigation animated:YES completion:nil];
    
}

- (UIImage *)imageWithColor:(UIColor *)color{
    
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
    
}

- (void)hide{
    
    if (self.animate) {
        
        CGPoint fromPoint = self.baseView.layer.position;
        CGPoint toPoint = CGPointMake(fromPoint.x, fromPoint.y + self.baseView.frame.size.height);
        
        CABasicAnimation *animation = [self animateBeginFrame:[NSValue valueWithCGPoint:fromPoint] toFrame:[NSValue valueWithCGPoint:toPoint] withDuration:0.1 stringPath:@"psition"];
        
        animation.delegate = self;
        
        [self.layer addAnimation:animation forKey:nil];
    } else {
        
        self.hidden = YES;
        [self removeFromSuperview];
        
    }
    
    
    
}


#pragma mark 判断相册，相机是否有使用权限

- (BOOL)judgeIsHavePhotoAblumAuthority{
    
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
        
        return NO;
        
    }
    
    return YES;
    
}


#pragma mark 请求所选择图片， 回调

- (void)requestSelPhotos:(UIViewController *)vController{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    
    __weak typeof(self) weakself = self;
    
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:self.arraySelectPhotos.count];
    for (int i = 0; i < self.arraySelectPhotos.count; i++) {
        
        [photos addObject:@""];
        
    }
    
    CGFloat scale = self.isSelectOriginalPhoto ? 1 : [UIScreen mainScreen].scale;
    NSLog(@"========scale = %f===selectarray = %@",scale,self.arraySelectPhotos);
    for (int i = 0; i < self.arraySelectPhotos.count; i++) {
        
        PhotoModel *model = self.arraySelectPhotos[i];
        [[PhotoTool sharePhotoTool] requestImageForAsset:model.asset scale:scale resizeMode:PHImageRequestOptionsResizeModeExact completion:^(UIImage *image) {
            
            if (image) {
                [photos replaceObjectAtIndex:i withObject:[weakself scaleImage:image]];
            }
            
            for (id obj in photos) {
                if ([obj isKindOfClass:[NSString class]]) {
                    return ;
                }
            }
            
            [hud hideAnimated:YES];
            
            [weakself done:photos];
            
            [weakself hide];
            
            [vController.navigationController dismissViewControllerAnimated:YES completion:nil];
            
        }];
        
    }
    
    
    
}

//对拿到的图片进行缩放，否则会内存暴涨
- (UIImage *)scaleImage:(UIImage *)image{
    
    CGSize size = CGSizeMake(1000, 1000 * image.size.height / image.size.width);
    
    UIGraphicsBeginImageContext(size);
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

- (void)done:(NSArray<UIImage *> *)photos{
    
    if (self.handler) {
        
        self.handler(photos, self.arraySelectPhotos.copy);
        self.handler = nil;
        
    }
    
}



- (void)showWithSender:(UIViewController *)vController animate:(BOOL)animate lastSelectPhotoModels:(NSArray<PhotoModel *> *)lastSelectPhotoModels completion:(void (^)(NSArray<UIImage *> * _Nonnull, NSArray<PhotoModel *> * _Nonnull))completion{
    
    self.handler = completion;
    self.animate = animate;
    self.sender = vController;
    
    [self.arraySelectPhotos removeAllObjects];
    [self.arraySelectPhotos addObjectsFromArray:lastSelectPhotoModels];
    
    [self loadPhotoFromAlbum];
    
    [self show];
    
}


- (void)loadPhotoFromAlbum{
    
    [self.arrayDataSources removeAllObjects];
    [self.arrayDataSources addObjectsFromArray:[[PhotoTool sharePhotoTool] getAllAssetInPhotoAblumWithAscending:NO]];
    
    [self.collectionView reloadData];
    
}

- (void)show{
    
    [self.sender.view addSubview:self];
    
    if (self.sender.tabBarController) {
        self.sender.tabBarController.tabBar.hidden = YES;
    }
    
    [self resetSubViewState];
    
    if (self.animate) {
        
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat heigh = [UIScreen mainScreen].bounds.size.height;
        CGPoint fromPoint = CGPointMake(width/2, heigh + 400/2);
        CGPoint toPoint = CGPointMake(width/2, heigh - 400/2);
        CABasicAnimation *animation = [self animateBeginFrame:[NSValue valueWithCGPoint:fromPoint] toFrame:[NSValue valueWithCGPoint:toPoint] withDuration:0.2 stringPath:@"position"];
        
        [self.layer addAnimation:animation forKey:nil];
        
        
    }
    
}

- (CABasicAnimation *)animateBeginFrame:(id)fromValue toFrame:(id)toValue withDuration:(CFTimeInterval)duration stringPath:(NSString *)keyPath{
    
    CABasicAnimation *animate = [CABasicAnimation animationWithKeyPath:keyPath];
    animate.fromValue = fromValue;
    animate.toValue = toValue;
    animate.duration = duration;
    animate.repeatCount = 0;
    animate.autoreverses = NO;
    
    
    //以下两个设置，保证了动画结束后，layer不会回到初始位置
    animate.removedOnCompletion = NO;
    animate.fillMode = kCAFillModeForwards;
    
    return animate;
    
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    self.hidden = YES;
    [self removeFromSuperview];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self hide];
    
}

- (void)resetSubViewState{
    
    self.hidden = NO;
    self.baseView.hidden = NO;
    
    if (self.arraySelectPhotos.count == 0) {
        
        [self.btnCamera setTitle:@"拍照" forState:UIControlStateNormal];
        [self.btnCamera setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    } else {
        
        [self.btnCamera setTitle:[NSString stringWithFormat:@"确定(%ld)",(unsigned long)self.arraySelectPhotos.count] forState:UIControlStateNormal];
        [self.btnCamera setTitleColor:[UIColor colorWithRed:19/255.0 green:153/255.0 blue:231/255.0 alpha:1.0] forState:UIControlStateNormal];
        
    }
    
    [self.collectionView setContentOffset:CGPointZero];
    
    
}


#pragma mark uicollectionDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return self.maxPreviewCount > self.arrayDataSources.count ? self.arrayDataSources.count : self.maxPreviewCount;
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifierCell = @"cell";
    
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: identifierCell forIndexPath:indexPath];
    
    cell.selectButton.selected = NO;
    
    PHAsset *asset = [[PHAsset alloc] init];
    asset = self.arrayDataSources[indexPath.row];
    
    
    __weak typeof(self) weakself = self;
    [self getImageWithAsset:asset completion:^(UIImage *image, NSDictionary *info) {
        
        cell.imagesVie.image = image;
        
        for (PhotoModel *model in weakself.arraySelectPhotos) {
            
            if ([model.localIdentifier isEqualToString:asset.localIdentifier]) {
                
                cell.selectButton.selected = YES;
                break;
            }
            
        }
        
    }];
    
    cell.selectButton.tag = indexPath.row;
    
    [cell.selectButton addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
    
}


#pragma mark UICollectionDelegate

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PHAsset *asset = self.arrayDataSources[indexPath.row];
    
    return [self getSizeWithAsset:asset];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ShowBigImageViewController *vController = [[ShowBigImageViewController alloc] init];
    vController.assets = self.arrayDataSources;
    vController.arraySelectPhotos = [NSMutableArray arrayWithArray:self.arraySelectPhotos];
    vController.selectIndex = indexPath.row;
    vController.maxSelectCount = self.maxSelectCount;
    vController.isPresent = YES;
    vController.shouldReverseAssets = YES;
    
    __weak typeof(self) weakself = self;
    __weak typeof(vController) weakBigController = vController;
    
    [vController setOnSelectedPhotos:^(NSArray<PhotoModel *> * selectPhotos, BOOL isSelectOriginalPhotos) {
       
        weakself.isSelectOriginalPhoto = isSelectOriginalPhotos;
        [weakself.arraySelectPhotos removeAllObjects];
        [weakself.arraySelectPhotos addObjectsFromArray:selectPhotos];
        
        [weakself changeButtonCameraTitle];
        [weakself.collectionView reloadData];
        
        
    }];
    
    [vController setBtnDoneBlock:^(NSArray<PhotoModel *> * seletPhotoModel, BOOL isSelectOriginal) {
        
        weakself.isSelectOriginalPhoto = isSelectOriginal;
        [weakself.arraySelectPhotos removeAllObjects];
        [weakself.arraySelectPhotos addObjectsFromArray:seletPhotoModel];
        [weakself requestSelPhotos:weakBigController];
        
    }];
    
    [self presentVC:vController];
    
    
}


#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    __weak typeof(self) weakself = self;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        if (weakself.handler) {
            
            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
            
            [[PhotoTool sharePhotoTool] saveImageToAblum:image completion:^(BOOL suc, PHAsset *asset) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    if (suc) {
                        
                        PhotoModel *model = [[PhotoModel alloc] init];
                        model.asset = asset;
                        model.localIdentifier = asset.localIdentifier;
                        weakself.handler(@[[weakself scaleImage:image]],@[model]);
                        
                    } else {
                        
                        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
                        hud.label.text = @"保存图片到相册失败";
                        
                        [hud hideAnimated:YES afterDelay:2];
                    }
                    
                    [hud hideAnimated:YES];
                    [weakself hide];
                    
                });
                
            }];
            
        }
        
    }];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    __weak typeof(self) weakself = self;
    [picker dismissViewControllerAnimated:YES completion:^{
        
        [weakself hide];
        
    }];
    
}

#pragma mark 获取图片尺寸及图片的相关方法
- (void)getImageWithAsset:(PHAsset *)asset completion:(void (^)(UIImage *image, NSDictionary *info))completion{
    
    CGSize size = [self getSizeWithAsset:asset];
    
    size.width *= 1.5;
    size.height *= 1.5;
    
    [[PhotoTool sharePhotoTool] requestImageForAsset:asset size:size resizeMode:PHImageRequestOptionsResizeModeFast completion:completion];
    
}

- (CGSize)getSizeWithAsset:(PHAsset *)asset{
    
    CGFloat width = (CGFloat)asset.pixelWidth;
    CGFloat height = (CGFloat)asset.pixelHeight;
    
    CGFloat scale = width/height;
    
    return CGSizeMake(self.collectionView.frame.size.height * scale, self.collectionView.frame.size.height);
    
}


- (void)buttonTouchUp:(UIButton *)sender{
    
    
    if (self.arraySelectPhotos.count >= self.maxSelectCount && sender.selected == NO) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
        hud.label.text = [NSString stringWithFormat:@"最多只能添加%ld张图片",(long)self.maxSelectCount];
        
        [hud hideAnimated:YES afterDelay:2];
        
        return ;
        
    }
    
    PHAsset *asset = self.arrayDataSources[sender.tag];
    
    if (!sender.selected) {
        
        CAKeyframeAnimation *animate = [self getButtonStatusChangedAnimation];
        
        [sender.layer addAnimation:animate forKey:nil];
        
        PhotoModel *model = [[PhotoModel alloc] init];
        model.asset = asset;
        model.localIdentifier = asset.localIdentifier;
        [self.arraySelectPhotos addObject:model];
        
        NSLog(@"array = %@",self.arraySelectPhotos);
        
    } else {
        
        for (PhotoModel *model in self.arraySelectPhotos) {
            
            if ([model.localIdentifier isEqualToString:asset.localIdentifier]) {
                
                [self.arraySelectPhotos removeObject:model];
                break;
                
            }
            
        }
    }
    
    sender.selected = !sender.selected;
    
    [self changeButtonCameraTitle];
    
    
}

- (void)changeButtonCameraTitle{
    
    if (self.arraySelectPhotos.count > 0) {
        
        [self.takephotoButton setTitle:[NSString stringWithFormat:@"确定(%ld)",(unsigned long)self.arraySelectPhotos.count] forState:UIControlStateNormal];
        
        [self.takephotoButton setTitleColor:[UIColor colorWithRed:19/255.0 green:153/255.0 blue:231/255.0 alpha:1.0] forState:UIControlStateNormal];
        
    } else {
        
        [self.takephotoButton setTitle:@"拍照" forState:UIControlStateNormal];
        [self.takephotoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    
    
}


- (CAKeyframeAnimation *)getButtonStatusChangedAnimation{
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.duration = 0.3;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    
    animation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.7, 0.7, 1.0)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    return animation;
    
}



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
